//
//  SearchResultViewController.m
//  AutoHome
//
//  Created by wqh on 15-5-21.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "SearchResultViewController.h"
#import "RecommendModel.h"
#import "RecommendCell.h"
#import "SearchDetailResultViewController.h"

@interface SearchResultViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,assign) NSInteger currentPage;
@property (nonatomic,assign) NSInteger rowcount;
@property (nonatomic) NSMutableArray *dataArr;
@property (nonatomic) UITableView *tableView;
@end

@implementation SearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentPage = 1;
    self.dataArr = [NSMutableArray array];
    self.title = @"搜索结果";
    self.view.backgroundColor = [UIColor purpleColor];
    [self createTableView];
    [self downloadData];
    
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBar.translucent = YES;
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 70;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"RecommendCell" bundle:nil] forCellReuseIdentifier:@"RecommendCellId"];
    
}

- (void)downloadData {
    [[DataEngine sharedInstance] requestSearchDataWithStr:self.searchStr Page:self.currentPage succsee:^(id respondsData) {
        [self downloadDataSuccess:respondsData];
    } failed:^(NSError *error) {
        
    }];
}

- (void)downloadDataSuccess:(id)resondsData {
    if (resondsData) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:resondsData options:NSJSONReadingMutableContainers error:nil];
        self.rowcount =  [dict[@"result"][@"rowcount"] integerValue];
        for (NSDictionary *artDict in dict[@"result"][@"hits"]) {
            RecommendModel *model = [[RecommendModel alloc] init];
           
            [model setValuesForKeysWithDictionary:artDict[@"data"]];
            [self.dataArr addObject:model];
        }
        [self.tableView reloadData];
    }
}

#pragma mark - UITabelViewDataSource 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
        
    RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecommendCellId" forIndexPath:indexPath];
    RecommendModel *model = self.dataArr[indexPath.row];
  
    [cell.img sd_setImageWithURL:[NSURL URLWithString:model.picurls] placeholderImage:[UIImage imageNamed:@"default_80_60"]];
    cell.titleLabel.text = model.title;
    cell.replycountLabel.text = [NSString stringWithFormat:@"%@评论",[model.replycount stringValue] ]  ;
    cell.timeLabel.text = model.date;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    RecommendNormalDetail *vc = [RecommendNormalDetail new];
    vc.model = self.dataArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
